package com.zhazha.stand.controller;



/**
 * Restful Controller
 * @author kevin
 *
 */
public abstract class RestfulController extends BaseController implements Restful {

	
	protected static final String DESTROY = "destroy";
	protected static final String UPDATE = "update";
	protected static final String CREATE = "create";
	
	protected static final String INDEX = "index";
	protected static final String SHOW = "show";
	protected static final String EDITNEW = "editNew";
	protected static final String EDIT = "edit";
	
	protected static final String XML = "xml";
	protected static final String JSON = "json";
	
	protected String resourceMethod;
	protected String resourceId;
	
	public String getResourceId() {
		return this.resourceId;
	}
	public void setResourceId(String resourceId) {
		this.resourceId = resourceId;
	}
	public String getResourceMethod() {
		return this.resourceMethod;
	}
	public void setResourceMethod(String resourceMethod) {
		this.resourceMethod = resourceMethod;
	}
	/**
	 * generated serial version uid
	 */
	private static final long serialVersionUID = 528957111099980002L;

	public String execute() {
		System.out.println(request.getMethod());
		String _method = request.getParameter("_method");
		if (_method == null) {
			_method = "";
		}
		if (request.getMethod().toLowerCase().equals("post")) {
			
			if (_method.toLowerCase().trim().equals("delete")) {
				return this.destroy();
			}
			if (_method.toLowerCase().trim().equals("put")) {
				return this.update();
			}
			
			// create
			return this.create();
		}
		
		if (request.getMethod().toLowerCase().equals("get")) {
			if (_method.equals("editNew")) {
				return this.editNew();
			}
			if (_method.equals("edit")) {
				int id = this.getIntParam("id");
				if (id == 0) {
					return this.editNew();
				} else {
					 return this.edit();
				}
			}
			
			int id = this.getIntParam("id");
			if (id != 0) {
				return this.show();
			} else {
				return this.index();
			}
		}
		return this.index();
	}

}
